library(tidyverse)
load("data/plotting_data.RData")

make_distplot <- function(threshold) {
    distplot <- ggplot() +
        geom_density(data = filter(urls_df, n_urls >= threshold), aes(x=url_score, linetype = 'URL', color = "URL"), linewidth = 2) +
        geom_density(data = filter(domains, n_urls >= threshold), aes(x=domain_score, linetype = 'Domain', color = "Domain"), linewidth = 2) + 
        coord_cartesian(xlim = c(-1, 1)) +
        scale_linetype_manual(values = c(1, 6)) +
        scale_color_manual(values = c("#984ea3", "#ff7f00")) +
        theme_classic()+
        theme(text = element_text(family = "serif", size = 12),
             plot.title = element_text(size = 16, face = "bold"),
             legend.position = "bottom",
             legend.direction = "horizontal",
             panel.grid.major.y = element_line(color="grey90"),
            panel.grid.minor.y = element_line(color="grey96")) +
        labs(x = "Audience Score", 
             y = "",
             color = "Type",
             linetype = 'Type',
            )
}

distplot <- make_distplot(quantile(domains$n_urls, 0.75))
ggsave(distplot, file = "results/fig_2a.pdf", height = 6, width = 6, dpi = 300)
ggsave(distplot, file = "results/tw_dist.pdf", height = 6, width = 6, dpi = 300)

distplot <- make_distplot(quantile(domains$n_urls, 0)) 
ggsave(distplot, file = "results/fig_i1.pdf", height = 6, width = 6, dpi = 300)
ggsave(distplot, file = "results/tw_dist_no_thresh.pdf", height = 6, width = 6, dpi = 300)

distplot <- make_distplot(quantile(domains$n_urls, 0.5)) 
ggsave(distplot, file = "results/fig_i2.pdf", height = 6, width = 6, dpi = 300)
ggsave(distplot, file = "results/tw_dist_median_thresh.pdf", height = 6, width = 6, dpi = 300)

distplot <- make_distplot(quantile(domains$n_urls, 0.9)) 
ggsave(distplot, file = "results/fig_i3.pdf", height = 6, width = 6, dpi = 300)
ggsave(distplot, file = "results/tw_dist_high_thresh.pdf", height = 6, width = 6, dpi = 300)
